package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.jvyou.app.common.domain.dto.RegionDto;
import com.jvyou.app.common.domain.entity.Region;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.ds.service.RegionService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 行政区域表(Region)表控制层
 *
 * @author 橘柚
 * @since 2025-03-11 17:26:40
 */
@RestController
@RequestMapping("/region")
public class RegionController {

    @Resource
    private RegionService regionService;

    @GetMapping("/list")
    @SaCheckPermission("system:region:list")
    public Result<List<Region>> list(@ModelAttribute RegionDto regionDto) {
        return Result.success(regionService.listRegion(regionDto));
    }

    @GetMapping("/page")
    @SaCheckPermission("system:region:page")
    public Result<PageResult<Region>> page(@ModelAttribute RegionDto regionDto) {
        return Result.success(regionService.pageRegion(regionDto));
    }

    @GetMapping("/regionTree")
    @SaCheckPermission("system:region:list")
    public Result<List<Map<String, Object>>> regionTree() {
        return Result.success(regionService.regionTree());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:region:get")
    public Result<Region> selectOne(@PathVariable Long id) {
        return Result.success(regionService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param region 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:region:insert")
    @CacheEvict(value = "regin", allEntries = true)
    public Result<Region> insert(@RequestBody Region region) {
        boolean success = regionService.save(region);
        return success ? Result.success(region) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    /**
     * 修改数据
     *
     * @param region 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("system:region:update")
    @CacheEvict(value = "regin", allEntries = true)
    public Result<Region> updateById(@RequestBody Region region) {
        boolean success = regionService.updateById(region);
        return success ? Result.success(region) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:region:delete")
    @CacheEvict(value = "regin", allEntries = true)
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = regionService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

    /**
     * 获取全国地级市区域
     *
     * @return 地级市
     */
    @GetMapping("/getCity")
    @SaIgnore
    public Result<List<String>> getCity() {
        return Result.success(regionService.getCity());
    }

}

